// pages/gang/productManage/productManage.js//index.js
//获取应用实例
const app = getApp()
var util = require('../../../utils/util.js')
var http = require('../../../utils/server.js')

Page({
  data: {
    productList: [],
    menuData: [],
    selectMenu: {
      current: -1,
      selectedCurrent: -1,
      selectedChild: [],
      selectedChildName: [],
      isShow: false,
    }
  },


  onLoad: function() {},

  onShow: function() {
    this.getData()
  },

  onParentEvent: function(e) {
    app.loginServer(app.globalData.code, e.detail.encryptedData, e.detail.iv, this.getData)
  },

  getData: function() {
    var that = this
    //商品列表
    http.post({
      url: "product/getProductList",
      type: ''
    }, function(data) {
      console.log('商品列表', data)
      that.setData({
        productList: data
      })
    })
    //筛选条件
    http.post({
      url: "product/getFilterData"
    }, function(data) {
      console.log('筛选条件', data)
      that.setData({
        menuData: data
      })
    })
  },
  menuTap: function(e) {
    var selectMenu = this.data.selectMenu;
    selectMenu.isShow = !selectMenu.isShow;
    if (selectMenu.current != e.currentTarget.dataset.index)
      selectMenu.isShow = true;
    selectMenu.current = e.currentTarget.dataset.index;
    this.setData({
      selectMenu: selectMenu
    })
  },

  childMenuTap: function(e) {
    var that = this;
    var selectMenu = this.data.selectMenu;
    selectMenu.isShow = false;
    selectMenu.selectedCurrent = e.currentTarget.dataset.pid;
    selectMenu.selectedChild[selectMenu.selectedCurrent] = e.currentTarget.dataset.index;
    selectMenu.selectedChildName[selectMenu.selectedCurrent] = e.currentTarget.dataset.name;
    this.setData({
      selectMenu: selectMenu
    })
    //商品列表
    http.post({
      url: "product/getProductList",
      // classify: selectMenu.selectedChild[0],
      type: selectMenu.selectedChild[0],
      brand: selectMenu.selectedChild[1],
    }, function(data) {
      console.log('商品列表', data)
      that.setData({
        productList: data
      })
    })

  },
  soldOut(e) {
    var that = this;
    var index = e.currentTarget.dataset.index
    var product = that.data.productList[index]
    wx.showModal({
      title: '操作提示',
      content: '确定下架该商品？(若需再次上架请在“上架商品”中操作)',
      success: function(res) {
        if (res.confirm) {
          //下架
          http.post({
            url: "product/soldOut",
            product_id: product.id,
          }, function(data) {
            util.showSuccess('下架成功', function() {
              var productList = that.data.productList
              productList.splice(index, 1)
              that.setData({
                productList: productList
              })
            })
          })
        }
      }
    })
  }

})